home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / lcppb.zip / LCPPLIB.ZIP / SELECTOR.CPP < prev    next >
C/C++ Source or Header  |  1991-07-04  |  4KB  |  135 lines

  1. // selector.cpp -- Selector class
  2.  
  3. #include "key.h"
  4. #include "selector.h"
  5.  
  6. #define KEY_ENTER 13    // <Enter> key value
  7. #define KEY_ESC   27    // <Esc> key value
  8. #define KEY_UP    -184  // <Cursor Up> key value
  9. #define KEY_DOWN  -176  // <Cursor Down> key value
  10. #define KEY_PGUP  -183  // <Page Up> key value
  11. #define KEY_PGDN  -175  // <Page Down> key value
  12.  
  13. /* -- Constructor. Passes arguments to the cwindow class's alternate
  14. constructor, and initializes its own data fields. */
  15.  
  16. selector::selector(winStruct &ws, const char *title, int popup)
  17.   : cwindow(ws, title)
  18. {
  19.   showHide = popup;       // Select pop-up or stationary style
  20.   selector::row = -1;     // i.e. not initialized
  21. }
  22.  
  23. /* -- Display current item at window row, col == 0, using the current
  24. attribute. */
  25.  
  26. void selector::showItem(int row)
  27. {
  28.   gotorc(row, 0);
  29.   cwindow::puts(((strItem *)currentItem())->getString());
  30.   eeol();
  31. }
  32.  
  33. /* -- Display current item at window row, col == 0 using the
  34. highlight attribute. This procedure displays the "selector bar." */
  35.  
  36. void selector::highlight(int row)
  37. {
  38.   reverseVideo();
  39.   showItem(row);
  40. }
  41.  
  42. /* -- Move selector bar up to previous item. Scrolls window contents
  43. down if necessary. */
  44.  
  45. void selector::moveUp(int &row)
  46. {
  47.   if (!listEmpty() && !atHeadOfList()) {
  48.     normalVideo();
  49.     showItem(row);
  50.     if (row > 0) row--; else scrollDown(1);
  51.     prevItem();
  52.     highlight(row);
  53.   }
  54. }
  55.  
  56. /* -- Move selector bar down to next item. Scrolls window contents up
  57. if necessary. */
  58.  
  59. void selector::moveDown(int &row)
  60. {
  61.   if (!listEmpty() && !atEndOfList()) {
  62.     normalVideo();
  63.     showItem(row);
  64.     if (row < ws.height - 3) row++; else scrollUp(1);
  65.     nextItem();
  66.     highlight(row);
  67.   }
  68. }
  69.  
  70. /* -- Display selector window, list items, and let operator move
  71. selector bar to any item, scrolling up and down as necessary if there
  72. are more items than can fit in the window. Returns a pointer to the
  73. selected item if operator presses <Enter>. Returns NULL if operator
  74. presses <Esc>. */
  75.  
  76. strItem *selector::getSelection(void)
  77. {
  78.   int key;    // Keypress value
  79.   int i;      // for-loop control variable
  80.  
  81.   if (listEmpty()) return NULL;
  82.   if (!isOpen) row = -1;  // Reset newly opened windows
  83.   showWindow();           // Make sure window is visible
  84.   normalVideo();          // Select normal text attribute
  85.   if (row < 0) {          // Initialize first time
  86.     row = 0;
  87.     resetList();
  88.     do {
  89.       showItem(row);
  90.       nextItem();
  91.     } while (!atHeadOfList() && (++row <= ws.height - 3));
  92.     row = 0;
  93.     resetList();
  94.   }
  95.   highlight(row);
  96.  
  97. /* -- Get keypress and move up or down. Return NULL for <Esc>, or
  98. pointer to selected item for <Enter>. */
  99.  
  100.   for (;;) {
  101.     switch (key = getKey()) {
  102.       case KEY_ESC:
  103.       case KEY_ENTER:
  104.         if (showHide) {
  105.           hideWindow();
  106.           row = -1;
  107.         }
  108.         if (key == KEY_ESC)
  109.           return NULL;
  110.         else
  111.           return (strItem *)currentItem();
  112.       case KEY_UP:
  113.         moveUp(row);
  114.         break;
  115.       case KEY_DOWN:
  116.         moveDown(row);
  117.         break;
  118.       case KEY_PGUP:
  119.         for (i = 0; i < (ws.height - 2) / 2; i++)
  120.           moveUp(row);
  121.         break;
  122.       case KEY_PGDN:
  123.         for (i = 0; i < (ws.height - 2) / 2; i++)
  124.           moveDown(row);
  125.         break;
  126.     }
  127.   }
  128. }
  129.  
  130.  
  131. // Copyright (c) 1990 by Tom Swan. All rights reserved
  132. // Revision 1.00    Date: 09/23/1990   Time: 05:39 pm
  133.  
  134.  
  135.